package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.SysRole;
import com.fx.service.SysRoleService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Auther: EDZ
 * @Date: 2018-5-24 16:51:
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
@Controller
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    SysRoleService sysRoleService;

    private static String pageTitle = "角色管理";
    private static String pageUrl = "/sysRole";

    @RequestMapping()
    public String intoView(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle,Constant.LIST_TITLE));
        return "sysRole/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(@RequestParam Map<String, Object> param) {
        Query query = new Query(param);
        List<SysRole> data = sysRoleService.listData(query);
        int count = sysRoleService.getCount(query);
        return renderPage(count, data);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "sysRole/add";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, Model model) {
        model.addAttribute("sysRole", sysRoleService.getById(id));
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        return "sysRole/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(SysRole sysRole) {
        String id = sysRole.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = sysRoleService.save(sysRole);
        } else {
            result = sysRoleService.updateById(sysRole);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public R batchDel(@RequestParam("ids") String[] ids) {
        if (sysRoleService.batchDelByIds(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/initSelect")
    @ResponseBody
    public List<SysRole> initSelect() {
        List<SysRole> sysRoleList = sysRoleService.getAllRole();
        return sysRoleList;
    }


}
